package com.amazon.ags.client.leaderboards;

import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.widget.RemoteViews;
import com.amazon.ags.api.ErrorCode;
import com.amazon.ags.api.RequestResponse;
import com.amazon.ags.api.leaderboards.Score;
import com.amazon.ags.client.BaseReplyMessengerFactory;
import com.amazon.ags.client.RequestResponseImp;
import com.amazon.ags.client.profiles.PlayerImpl;
import com.amazon.ags.constants.LeaderboardFilter;
import com.amazon.ags.constants.ScoreFormat;
import com.amazon.ags.overlay.PopUpContent;
import com.amazon.ags.overlay.PopUpManager;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;

public class LeaderboardsReplyMessengerFactory<T extends RequestResponse> extends BaseReplyMessengerFactory<T>
{
  private static final String FEATURE_NAME = "LB";
  private static final String TAG = "LB_" + LeaderboardsReplyMessengerFactory.class.getSimpleName();
  private static final String UNKNOWN_VALUE = "UNKNOWN";

  public LeaderboardsReplyMessengerFactory(Context paramContext)
  {
    super(paramContext);
  }

  private final void generateSubmitScorePopUp(RemoteViews paramRemoteViews, int paramInt, String paramString)
  {
    Log.d(TAG, "Entering generateSubmitScorePopUp with RemoteViews " + paramRemoteViews);
    Bundle localBundle = new Bundle();
    localBundle.putString("LEADERBOARD_ID", paramString);
    PopUpContent localPopUpContent = new PopUpContent(paramInt, paramRemoteViews, localBundle);
    this.popUpManager.executePopUp(localPopUpContent);
  }

  protected final T processMessage(Message paramMessage)
  {
    Bundle localBundle = paramMessage.getData();
    int i = paramMessage.arg1;
    Log.d(TAG, "Processing incoming service response message of type: " + paramMessage.what + " and responseCode: " + i);
    switch (paramMessage.what)
    {
    default:
      Log.e(TAG, "Illegal value received for request type parameter: " + paramMessage.what);
      throw new IllegalArgumentException("Received an invalid value for request type parameter");
    case 8:
      if (i != 17)
      {
        int n = paramMessage.arg2;
        Log.d(TAG, "The service request was a failure with code " + n + ", constructing failure response");
        return new SubmitScoreResponseImp(n, ErrorCode.fromServiceResponseCode(n));
      }
      return unbundleSubmitScoreResponse(localBundle, i);
    case 9:
      if (i != 17)
      {
        int m = paramMessage.arg2;
        Log.d(TAG, "The service request was a failure with code " + m + ", constructing failure response");
        return new GetLeaderboardsResponseImp(m, ErrorCode.fromServiceResponseCode(m));
      }
      return unbundleRequestLeaderboardsResponse(localBundle, i);
    case 7:
      if (i != 17)
      {
        int k = paramMessage.arg2;
        Log.d(TAG, "The service request was a failure with code " + k + ", constructing failure response");
        return new GetScoresResponseImp(k, ErrorCode.fromServiceResponseCode(k));
      }
      return unbundleRequestScoresResponse(localBundle, i);
    case 10:
      if (i != 17)
      {
        int j = paramMessage.arg2;
        Log.d(TAG, "The service request was a failure with code " + j + ", constructing failure response");
        return new GetPlayerScoreResponseImp(j, ErrorCode.fromServiceResponseCode(j));
      }
      return unbundleRequestUserScoreResponse(localBundle, i);
    case 27:
    case 28:
      if (i != 17)
      {
        Log.d(TAG, "The service request was a failure with code " + 19 + ", constructing failure response");
        return new RequestResponseImp(19, ErrorCode.fromServiceResponseCode(19));
      }
      return unbundleRequestResponse(localBundle, i);
    case 31:
    }
    if (i != 17)
    {
      Log.d(TAG, "The service request was a failure with code " + 19 + ", constructing failure response");
      return new GetLeaderboardPercentilesResponseImp(19, ErrorCode.fromServiceResponseCode(19));
    }
    return unbundleRequestPercentilesResponse(localBundle, i);
  }

  protected final T unbundleRequestLeaderboardsResponse(Bundle paramBundle, int paramInt)
  {
    String[] arrayOfString1 = paramBundle.getStringArray("LEADERBOARD_GET_BOARDS_BUNDLE_NAMES");
    String[] arrayOfString2 = paramBundle.getStringArray("LEADERBOARD_GET_BOARDS_BUNDLE_IDS");
    String[] arrayOfString3 = paramBundle.getStringArray("LEADERBOARD_DISPLAY_TEXT");
    String[] arrayOfString4 = paramBundle.getStringArray("LEADERBOARD_DATA_FORMAT");
    if (arrayOfString3 == null)
    {
      arrayOfString3 = new String[arrayOfString2.length];
      Arrays.fill(arrayOfString3, "UNKNOWN");
    }
    if (arrayOfString4 == null)
    {
      arrayOfString4 = new String[arrayOfString3.length];
      Arrays.fill(arrayOfString4, ScoreFormat.UNKNOWN.toString());
    }
    if ((arrayOfString1.length != arrayOfString2.length) || (arrayOfString2.length != arrayOfString3.length) || (arrayOfString3.length != arrayOfString4.length))
      return new GetLeaderboardsResponseImp(19, ErrorCode.DATA_VALIDATION_ERROR);
    ArrayList localArrayList = new ArrayList();
    int i = 0;
    while (i < arrayOfString1.length)
      try
      {
        ScoreFormat localScoreFormat = ScoreFormat.valueOf(arrayOfString4[i]);
        localArrayList.add(new LeaderboardImpl(arrayOfString2[i], arrayOfString1[i], arrayOfString3[i], localScoreFormat));
        i++;
      }
      catch (IllegalArgumentException localIllegalArgumentException)
      {
        return new GetLeaderboardsResponseImp(19, ErrorCode.DATA_VALIDATION_ERROR);
      }
      catch (NullPointerException localNullPointerException)
      {
        return new GetLeaderboardsResponseImp(19, ErrorCode.DATA_VALIDATION_ERROR);
      }
    return new GetLeaderboardsResponseImp(localArrayList, paramInt);
  }

  protected final T unbundleRequestPercentilesResponse(Bundle paramBundle, int paramInt)
  {
    long[] arrayOfLong = paramBundle.getLongArray("LEADERBOARD_SCORES_ARRAY");
    int[] arrayOfInt = paramBundle.getIntArray("LEADERBOARD_PERCENTILES_ARRAY");
    String[] arrayOfString = paramBundle.getStringArray("LEADERBOARD_PLAYER_ALIASES_ARRAY");
    String str1 = paramBundle.getString("LEADERBOARD_ID");
    String str2 = paramBundle.getString("LEADERBOARD_NAME");
    String str3 = paramBundle.getString("LEADERBOARD_DISPLAY_TEXT");
    String str4 = paramBundle.getString("LEADERBOARD_DATA_FORMAT");
    int i = paramBundle.getInt("LEADERBOARD_USER_INDEX", -1);
    if ((arrayOfLong == null) || (arrayOfInt == null) || (arrayOfString == null) || (TextUtils.isEmpty(str1)) || (TextUtils.isEmpty(str2)) || (TextUtils.isEmpty(str4)))
      return new GetLeaderboardPercentilesResponseImp(19, ErrorCode.DATA_VALIDATION_ERROR);
    if ((arrayOfLong.length != arrayOfInt.length) || (arrayOfInt.length != arrayOfString.length) || (i < -1) || (i >= arrayOfString.length))
      return new GetLeaderboardPercentilesResponseImp(19, ErrorCode.DATA_VALIDATION_ERROR);
    try
    {
      LeaderboardImpl localLeaderboardImpl = new LeaderboardImpl(str1, str2, str3, ScoreFormat.valueOf(str4));
      ArrayList localArrayList = new ArrayList(arrayOfLong.length);
      int j = arrayOfLong.length;
      for (int k = 0; k < j; k++)
        localArrayList.add(new LeaderboardPercentileItemImp(arrayOfString[k], arrayOfLong[k], arrayOfInt[k]));
      GetLeaderboardPercentilesResponseImp localGetLeaderboardPercentilesResponseImp = new GetLeaderboardPercentilesResponseImp(localLeaderboardImpl, localArrayList, i, paramInt);
      return localGetLeaderboardPercentilesResponseImp;
    }
    catch (IllegalArgumentException localIllegalArgumentException)
    {
      return new GetLeaderboardPercentilesResponseImp(19, ErrorCode.DATA_VALIDATION_ERROR);
    }
    catch (NullPointerException localNullPointerException)
    {
    }
    return new GetLeaderboardPercentilesResponseImp(19, ErrorCode.DATA_VALIDATION_ERROR);
  }

  public final T unbundleRequestResponse(Bundle paramBundle, int paramInt)
  {
    return new RequestResponseImp(paramInt, ErrorCode.NONE);
  }

  protected final T unbundleRequestScoresResponse(Bundle paramBundle, int paramInt)
  {
    long[] arrayOfLong = paramBundle.getLongArray("LEADERBOARD_SCORES_ARRAY");
    int[] arrayOfInt = paramBundle.getIntArray("LEADERBOARD_RANKS_ARRAY");
    String[] arrayOfString = paramBundle.getStringArray("LEADERBOARD_PLAYER_ALIASES_ARRAY");
    String str1 = paramBundle.getString("LEADERBOARD_DISPLAY_TEXT");
    ScoreFormat localScoreFormat = ScoreFormat.valueOf(paramBundle.getString("LEADERBOARD_DATA_FORMAT"));
    String str2 = paramBundle.getString("LEADERBOARD_NAME");
    String str3 = paramBundle.getString("LEADERBOARD_ID");
    Score[] arrayOfScore = new Score[arrayOfLong.length];
    for (int i = 0; i < arrayOfLong.length; i++)
    {
      PlayerImpl localPlayerImpl = new PlayerImpl(arrayOfString[i]);
      arrayOfScore[i] = new ScoreImp(arrayOfLong[i], localPlayerImpl, arrayOfInt[i], str3);
    }
    return new GetScoresResponseImp(arrayOfScore, str1, localScoreFormat, str2, str3, paramInt);
  }

  protected final T unbundleRequestUserScoreResponse(Bundle paramBundle, int paramInt)
  {
    return new GetPlayerScoreResponseImp(paramBundle.getLong("LEADERBOARD_SUBMIT_SCORE"), paramBundle.getInt("LEADERBOARD_USER_RANK"), paramInt);
  }

  protected final T unbundleSubmitScoreResponse(Bundle paramBundle, int paramInt)
  {
    String[] arrayOfString = paramBundle.getStringArray("LEADERBOARD_SUBMIT_RESULT");
    HashMap localHashMap1 = new HashMap();
    HashMap localHashMap2 = new HashMap();
    int i = arrayOfString.length;
    for (int j = 0; ; j++)
      if (j < i)
      {
        String str2 = arrayOfString[j];
        try
        {
          JSONObject localJSONObject = new JSONObject(str2);
          Log.v(TAG, "Parsing json element: " + localJSONObject);
          String str3 = localJSONObject.getString("Scope");
          if (LeaderboardFilter.valueOf(str3) == null)
          {
            Log.w(TAG, "Invalid filter returned from service: " + str3);
          }
          else
          {
            LeaderboardFilter localLeaderboardFilter = LeaderboardFilter.valueOf(str3);
            int m = localJSONObject.getInt("PlayerRank");
            localHashMap1.put(localLeaderboardFilter, Boolean.valueOf(localJSONObject.getBoolean("IsImproved")));
            localHashMap2.put(localLeaderboardFilter, Integer.valueOf(m));
          }
        }
        catch (JSONException localJSONException)
        {
          Log.w(TAG, "Caught JSON Exception, skipping this Submit Score Result Element");
        }
      }
      else
      {
        SubmitScoreResponseImp localSubmitScoreResponseImp = new SubmitScoreResponseImp(localHashMap1, localHashMap2, paramInt);
        Log.d(TAG, "Created SubmitScoreResponse: " + localSubmitScoreResponseImp);
        int k = paramBundle.getInt("LEADERBOARD_OVERLAY_CALLBACK_LAYOUT");
        RemoteViews localRemoteViews = (RemoteViews)paramBundle.getParcelable("LEADERBOARD_POP_UP_LAYOUT");
        String str1 = paramBundle.getString("LEADERBOARD_ID");
        if (localRemoteViews != null)
          generateSubmitScorePopUp(localRemoteViews, k, str1);
        return localSubmitScoreResponseImp;
      }
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.amazon.ags.client.leaderboards.LeaderboardsReplyMessengerFactory
 * JD-Core Version:    0.6.2
 */